package com.zb.web.controller;

import com.zb.common.SendSms;
import com.zb.model.SysDes;
import com.zb.model.TUser;
import com.zb.service.NoticeService;
import com.zb.service.impl.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/web")
public class LoginController {

	@Autowired
	private LoginServiceImpl ls;
	
	@Autowired
	private NoticeService cs;
	
	/**
	 * ǰ��Ĭ�����
	 * <p>Title: index</p>  
	
	 * <p>Description: </p>  
	
	 * @return
	 */
	@RequestMapping("/index")
	public String index() {
		return "login";
	}
	
	/**
	 * @param phone
	 * @param password
	 * @return
	 */
	@RequestMapping("/loginInspect")
	public String loginInspect(String phone,String password,HttpSession session,HttpServletResponse response) {
		TUser user=ls.loginInspectService(phone, password);
		if(user!=null) {
			session.setAttribute("user", user);
			session.setAttribute("username", user.getUsername());
			session.setAttribute("userid", user.getUserId());
			session.setAttribute("email", user.getEmail());
			session.setAttribute("phone", user.getPhone());	
			
			List<SysDes> noticeList=cs.selectCate();
			session.setAttribute("noticeList", noticeList);
			
			try {
				response.sendRedirect("SelectCategory");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "homePage";
		}else {
			
		}
			//JOptionPane.showMessageDialog(null, "please check the password!");
			return"login";
	}
	
	/**
	 * �û�ע��
	 * @param phone
	 * @param password
	 * @param smscode
	 * @return
	 */
	@RequestMapping("/register")
	public String register(String phone,String smscode,String password,String username,String email,HttpServletResponse response) {
		if(ls.registerService(phone, password,username,email)) {
//			JOptionPane.showMessageDialog(null, "ע��ɹ�");
//			try {
//				response.sendRedirect("../jsp/login.jsp");
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			return "login";
		}else {
			//JOptionPane.showMessageDialog(null, "the number is registed");
			try {
				response.sendRedirect("jsp/login.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("Controller get here");
		return "login";
	}
	
	/**
	 * �û��޸�����
	 * @param phone
	 * @param password
	 * @param sms
	 * @return
	 */
	@RequestMapping("/resetpsw")
	public String resetpsw(String phone,String sms,String password) {
		ls.resetpswService(phone,password);
		return "login";
	}
	
	/**
	 * ���������֤��ע��
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/sms.do")
	public void sendSms(HttpServletRequest request,HttpServletResponse response) throws IOException {
		System.out.println("this sms do");
		String SMS = "SMS_174994633";	//ע����
		String randomSMSCode = SendSms.getRandomSMSCode(6,true);
		String phoneNumber = request.getParameter("phoneNum");
		System.out.println("-------------"+phoneNumber);
		SendSms.sendSms(phoneNumber,randomSMSCode,SMS);
		//System.out.println("-------======"+"�������");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		session.setAttribute("randomSMSCode", randomSMSCode);
		session.setAttribute("createTime", System.currentTimeMillis());
		out.write("1");
	}
	
	/**
	 * ���������֤���һ�����
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/ssms.do")
	public void sendSmsr(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String SMS = "SMS_175490570";	//�޸�������
		String randomSMSCode = SendSms.getRandomSMSCode(6,true);
		
		String phoneNumber = request.getParameter("phoneNum");
		SendSms.sendSms(phoneNumber,randomSMSCode,SMS);
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		session.setAttribute("randomSMSCodes", randomSMSCode);
		session.setAttribute("createTimes", System.currentTimeMillis());
		out.write("1");
	}

	/**
	 * �û����ĸ�����Ϣ
	 * @param request
	 * @param userid
	 * @param phone
	 * @param email
	 * @param username
	 * @return
	 */
	@RequestMapping("/updatauser")
	public String rename(HttpServletRequest request,String userid,String phone,String email,String username) {
		HttpSession session = request.getSession();
		userid=session.getAttribute("userid").toString();
		phone=request.getParameter("phone");
		email=request.getParameter("email");
		username=request.getParameter("username");
		ls.renameService(userid,phone,username,email);
		session.setAttribute("username", username);
		session.setAttribute("userid", userid);
		session.setAttribute("email", email);
		session.setAttribute("phone", phone);
		//JOptionPane.showMessageDialog(null, "�޸ĳɹ�");
		//request.getRequestDispatcher("jsp/udai_setting.jsp");
		return "udai_setting";
	}

	/**
	 * �û�����������֤old
	 * @param phone
	 * @param email
	 * @param username
	 * @return
	 */
	@RequestMapping("/yanzhengold")
	public String yanzhengold(HttpServletRequest request,String userid,String password) {
		HttpSession session = request.getSession();
		userid=session.getAttribute("userid").toString();
		password=request.getParameter("password");
		boolean flag=ls.yanzhengoldService(userid,password);
		if(flag) {
			return "udai_modifypwd_step2";
		}else {
			//JOptionPane.showMessageDialog(null, "password is wrong");
			return "udai_modifypwd_step1";
		}
		
	}
	
	/**
	 * �û���������
	 * @param phone
	 * @return
	 */
	@RequestMapping("/repassword")
	public String repassword(HttpServletRequest request,String userid,String newpassword) {
		HttpSession session = request.getSession();
		userid=session.getAttribute("userid").toString();
		String password1=request.getParameter("password1");
		String password2=request.getParameter("password2");
		if(!password1.equals(password2)) {
			JOptionPane.showMessageDialog(null, "two pwd is not consist");
			return "udai_modifypwd_step2";
		}
		newpassword=password1;
		boolean flag=ls.repwService(userid,newpassword);
		if(flag) {
			//JOptionPane.showMessageDialog(null, "���óɹ�");
			return "udai_modifypwd_step3";
		}else {
			JOptionPane.showMessageDialog(null, "repassword wrong");
			return "udai_modifypwd_step2";
		}
		
	}
}
